(function(){
	var obj={};
	obj.ajax=function(type,url,data,headers,context,callback){
		$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
		.done(callback);
	};
	window.j=obj;
})();

function showFoodComponent(template){
	this.model=null;
	this.template=template;
	this.findFoods=function(){
		j.ajax("GET","findFoods",null,null,this,function(customers){
			this.model=customers;
			_render(this.model);
		});
	};
	this.findFoodsByIcnumAndType=function(type,icnum){
//		j.ajax("GET","findFoodsByIcnumAndType?type="+type+"&icnum="+icnum,null,null,this,function(foodinfo){
			j.ajax("GET","findFoodsByIcnumAndType/"+icnum+"?type="+type,null,null,this,function(foodinfo){
			this.model=foodinfo;
			_render(this.model);
		});
	};
	this.findFoodsByInputText=function(text,icnum){
		j.ajax("GET","findFoodsByInputText?text="+text+"&icnum="+icnum,null,null,this,function(foods){
			this.model=foods;
			_render(this.model);
		});
	}
	this.findAllFoods=function(){
		j.ajax("GET","findAllFoods",null,null,this,function(foods){
			this.model=foods;
			_render(this.model);
		});
		
	};
	function _render(data){
		template.empty();
		data.forEach(function(item){
			$("#foodTemplate").clone(true)
			.css("display","block")
			.find(".imageTemplate").attr("src",item.fimg).end()
			.find(".fnameTemplate").text(item.fname).end()
			.find(".fpriceTemplate").text(item.fprice+"￥").css("color","red").end()
			.find(".orderTemplate").attr("fid",item.fid).end()
			.appendTo(template);
		});
	}
	
}

function showCommentComponent(template){
	this.model=null;
	this.template=template;
	this.findCommondByIcnum=function(icnum){
			j.ajax("GET","findMerchantCommentList/"+icnum,null,null,this,function(commentList){
			this.model=commentList;
			_render(this.model);
		});
	};
	
	function _render(data){
		template.empty();
		var t = $("<table class='table table-hover'><thead><th>用户</th><th>评论内容</th><th>评分</th><th>评论时间</th><th>商家回复信息</th><th>商家回复时间</th></thead></table>");
		
		t.appendTo(template);
		
		var tbody=$("<tbody></tbody>");
		data.forEach(function(item){
			var ctime = Common.formatDate(item.ctime);
			var rtime = Common.formatDate(item.rtime);
			var reply=item.reply;
			if(!reply){
				reply = "尚未回复此订单的评论";
			}
			var tr = $("<tr>");
			tr.append($("<td></td>").text(item.client.cname))
				 .append($("<td></td>").text(item.comments))
				 .append($("<td></td>").text(item.oclevel))
				 .append($("<td></td>").text(ctime))
				 .append($("<td></td>").text(reply))
				 .append($("<td></td>").text(rtime)).appendTo(tbody);
		});
		tbody.appendTo(t);
		
	};
	
}


$(function(){
	var c=new showFoodComponent($("#foodDiv"));
	var comments = new showCommentComponent($("#foodDiv"));
	$("#allFoods").on("click",function(){
		
	});
	$(".sub-menu li").on("click",function(){
		var type = $(this).attr("type");
		var icnum = $(this).attr("icnum");
		c.findFoodsByIcnumAndType(type,icnum);
	});
	$("#findText").bind('input propertychange',function(){
		var text = $(this).val();
		var icnum = $("#merchantFlag").attr("icnum");
		c.findFoodsByInputText(text,icnum);
	});
	$("#commontTag").click(function(e){
		var icnum  = $(this).attr("icnum");
		comments.findCommondByIcnum(icnum);
	});
	
	
  
});

